package cn.jh.common.utils;


import com.google.common.collect.ImmutableMap;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;


public class ResultWrap {

    public static Map<String, String> add(Map<String, String> map, String key, String value) {
        map.put(key, value);
        return map;
    }

    public static Map<String, Object> ok() {
        return ok("OK");
    }

    public static Map<String, Object> ok(String message) {
        return ImmutableMap.of(
                Constss.RESP_CODE, Constss.SUCCESS,
                Constss.RESP_MESSAGE, message
        );
    }

    /**
     * 正确返回给定的消息和数据结果。
     *
     * @param message 返回的消息 。
     * @param result 返回的结果。
     * @return 返回一个Map表示正确返回给定的消息和数据结果。
     */
    public static Map<String, Object> ok(@NotNull String message, @NotNull Object result) {
        return ImmutableMap.of(
                Constss.RESP_CODE, Constss.SUCCESS,
                Constss.RESP_MESSAGE, message,
                Constss.RESULT, result
        );
    }

    public static Map<String, Object> init(String respCode, String respMesg) {
        return init(respCode, respMesg, null);
    }


    public static Map<String, Object> init(String respCode, String respMesg, String result) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (result == null || result.equalsIgnoreCase("")) {
            map.put(Constss.RESULT, null);
        } else {
            map.put(Constss.RESULT, result);
        }

        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        return map;
    }


    public static Map<String, Object> init(String respCode, String respMesg, Object result) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        return map;
    }


    public static Map<String, String> info(Logger logger, String respCode, String respMesg) {
        return info(logger, respCode, respMesg, null);
    }


    public static Map<String, String> info(Logger logger, String respCode, String respMesg, String result) {
        Map<String, String> map = new HashMap<String, String>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.info("{}", map);
        return map;
    }


    public static Map<String, Object> info(Logger logger, String respCode, String respMesg, Object result) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.info("{}", map);
        return map;
    }


    public static Map<String, String> warn(Logger logger, String respCode, String respMesg) {
        return warn(logger, respCode, respMesg, null);
    }


    public static Map<String, String> warn(Logger logger, String respCode, String respMesg, String result) {
        Map<String, String> map = new HashMap<String, String>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.warn("{}", map);
        return map;
    }


    public static Map<String, Object> warn(Logger logger, String respCode, String respMesg, Object result) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.warn("{}", map);
        return map;
    }


    public static Map<String, Object> err(Logger logger, String respCode, String respMesg) {
        return err(logger, respCode, respMesg, null);
    }


    public static Map<String, Object> err(Logger logger, String respCode, String respMesg, String result) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.error("{}", map);
        return map;
    }


    public static Map<String, Object> err(Logger logger, String respCode, String respMesg, Object result) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put(Constss.RESULT, result);
        map.put(Constss.RESP_CODE, respCode);
        map.put(Constss.RESP_MESSAGE, respMesg);

        logger.error("{}", map);
        return map;
    }

}
